<?php
/**
 * Created by PhpStorm.
 * User: Ason
 * Date: 15-6-12
 * Time: 下午4:29
 *
 * 前台导航设置
 */

class Navi extends Admin_controller {
	
	private $_json = array( 'status'=>'error', 'error'=>'未知错误' );

	public function __construct(){
		parent::__construct();
		parent::logged_check();
	}

	/**
	 * 导航管理
	 * */
	public function index(){

		$this->view( 'navi/index' );
	}

	/**
	 * 导航分组
	 * */
	public function group(){

		$this->view( 'navi/navi' );
	}
	
	/**
	 * 后台菜单管理
	 * */
	public function menu( $parent_id = 0 ){
		
		$this->load->model( 'sidebar_model' );
		$this->data[ 'table_menu' ] = $this->sidebar_model->get_menus_by_parent_id( $parent_id );
		$this->data[ 'parent_name' ] = $parent_id ? $this->sidebar_model->get_menu_by_id( $parent_id )->name : '无';
		$this->data[ 'top_menu' ] = $this->sidebar_model->get_menus_by_parent_id();
		
		$this->view( 'navi/menu' );
	}
	/**
	 * 编辑菜单项
	 * */
	public function edit_menu( $id ){
		
		if( $id ){
			$this->load->model( 'sidebar_model' );
			$this->data[ 'menu_info' ] = $this->sidebar_model->get_menu_by_id( $id );
			$this->data[ 'top_menu' ] = $this->sidebar_model->get_menus_by_parent_id();
			$this->data[ 'id' ] = $id;
			
			$this->load->view( 'navi/part/edit_menu', $this->data );
		}
	}
	/**
	 * 执行更新菜单项ajax-post
	 * */
	public function do_edit_menu(){
		
		$post = $this->input->post( null, true );
		
		if( $this->check_menu_input( $post ) ){
			$this->load->model( 'sidebar_model' );
			if( $this->sidebar_model->update_menu( $post ) ){
				$this->_json[ 'status' ] = 'success';
				$this->_json[ 'error' ] = '菜单修改成功';
			}
		}
				
		echo json_encode( $this->_json );
		return ;
	}
	/*
	 * 添加后台管理菜单
	 * */
	public function do_add_menu(){
		
		$post = $this->input->post( null, TRUE );
		
		if( $this->check_menu_input( $post ) ){
			$this->load->model( 'sidebar_model' );
			if( $this->sidebar_model->add_menu( $post ) ){
				$this->_json[ 'status' ] = 'success';
				$this->_json[ 'error' ] = '新增菜单成功';
			}
		}
		echo json_encode( $this->_json );
		return FALSE;
		
	}
	
	/*
	 * 删除后台管理菜单
	 * */
	public function do_del_menu( $id ){
		
		$id = (int)$id;
		$this->load->model( "sidebar_model" );
		if( $id == 0 || ! $this->sidebar_model->get_menu_by_id( $id ) ){
			$this->_json[ 'error' ] = '菜单不存在，请返回重新操作！';
			echo json_encode( $this->_json );
			return;
		}
		
		if( $this->sidebar_model->get_menus_by_parent_id( $id ) ){
			$this->_json[ 'error' ] = '当前菜单下还有子菜单，请删除全部子菜单后再进行该操作！';
			echo json_encode( $this->_json );
			return;
		}
		
		if( $this->sidebar_model->del_menu( $id ) ){
			$this->_json[ 'status' ] = 'success';
			$this->_json[ 'error' ] = '删除菜单成功';
			echo json_encode( $this->_json );
			return;
		}		
	}
	
	/*
	 * 验证输入的菜单数据
	 * @return bool
	 * */
	private function check_menu_input( $arr ){
		
		if( empty( $arr[ 'name' ] ) ){
			$this->_json[ 'error' ] = '菜单名称不能为空';
			return false;
		}
		if( empty( $arr[ 'url' ] ) ){
			$this->_json[ 'error' ] = '路径不能为空';
			return false;
		}
		if( empty( $arr[ 'url' ] ) ){
			$this->_json[ 'error' ] = '路径不能为空';
			return false;
		}

		return TRUE;
	}


} 